package com.xbts.web.controller.party;

import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.core.page.TableDataInfo;
import com.xbts.common.enums.BusinessType;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.system.domain.PartyPointsDeclaration;
import com.xbts.system.domain.vo.PartyPointsDeclarationVo;
import com.xbts.system.service.IPartyPointsDeclarationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 积分申报Controller
 *
 * @author xbts
 * @date 2023-06-12
 */
@Api(tags = "积分申报")
@RestController
@RequestMapping("/party/declaration")
public class PartyPointsDeclarationController extends BaseController {

    @Autowired
    private IPartyPointsDeclarationService partyPointsDeclarationService;

    /**
     * 查询积分申报
     */
    @ApiOperation("查询积分申报列表")
    @PreAuthorize("@ss.hasPermi('party:list:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PartyPointsDeclaration partyPointsDeclaration) {
        startPage(partyPointsDeclaration);
        List<PartyPointsDeclaration> list = partyPointsDeclarationService.selectPartyPointsDeclarationList(partyPointsDeclaration);
        return getDataTable(list);
    }

    /**
     * 导出积分申报
     */
    @ApiOperation("导出积分申报")
    @PreAuthorize("@ss.hasPermi('party:list:export')")
    @Log(title = "积分申报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartyPointsDeclaration partyPointsDeclaration) {
        List<PartyPointsDeclaration> list = partyPointsDeclarationService.selectPartyPointsDeclarationList(partyPointsDeclaration);
        ExcelUtil<PartyPointsDeclaration> util = new ExcelUtil<PartyPointsDeclaration>(PartyPointsDeclaration.class);
        util.exportExcel(response, list, "积分申报数据");
    }

    /**
     * 获取积分申报详细信息
     */
    @ApiOperation("获取积分申报详细信息")
    @ApiImplicitParam(name = "id", value = "积分申报ID", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('party:list:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(partyPointsDeclarationService.selectPartyPointsDeclarationById(id));
    }

    /**
     * 新增积分申报
     */
    @ApiOperation("新增积分申报")
    @PreAuthorize("@ss.hasPermi('party:list:add')")
    @Log(title = "积分申报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PartyPointsDeclaration partyPointsDeclaration) {
        return toAjax(partyPointsDeclarationService.insertPartyPointsDeclaration(partyPointsDeclaration));
    }

    /**
     * 修改积分申报
     */
    @ApiOperation("修改积分申报")
    @PreAuthorize("@ss.hasPermi('party:list:edit')")
    @Log(title = "积分申报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PartyPointsDeclaration partyPointsDeclaration) {
        boolean admin = SecurityUtils.isAdmin();
        if (!admin) {
            throw new ServiceException("只允许超级管理员用户操作");
        }
        return toAjax(partyPointsDeclarationService.updatePartyPointsDeclaration(partyPointsDeclaration));
    }

    /**
     * 删除积分申报
     */
    @ApiOperation("删除积分申报")
    @ApiImplicitParam(name = "ids", value = "积分申报ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('party:list:remove')")
    @Log(title = "积分申报", businessType = BusinessType.DELETE)
    @PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> ids) {
        return toAjax(partyPointsDeclarationService.deletePartyPointsDeclarationByIds(ids));
    }

    /**
     * 积分排行榜
     */
    @ApiOperation("积分排行榜")
    @ApiImplicitParam(name = "year", value = "积分申报ID集合", dataType = "String", paramType = "query", dataTypeClass = String.class)
    @GetMapping("/ranking")
    public TableDataInfo ranking(String year) {
        startPage();
        List<PartyPointsDeclarationVo> list = partyPointsDeclarationService.pointsRanking(year);
        return getDataTable(list);
    }


}
